﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Newtonsoft.Json;
using Client.Models;
using Manage;

namespace Client
{
    public partial class FrmRegister : Form
    {
        public FrmRegister()
        {
            InitializeComponent();
        }
        private List<AreaProvinceModel> prList;
        private List<AreaCityModel> ctList;
        private List<AreaDistrictModel> dtList;
        private List<SBProvinceModel> sbprList;
        private List<SBCityModel> sbctList;
        private List<SBDistrictModel> sbdtList;
        private bool isInit;
        private bool isSBInit;

        private void tbxLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if(string.IsNullOrEmpty(tbxUserName.Text))
                    throw new Exception("用户名不能为空！");
                if(string.IsNullOrEmpty(tbxPassword.Text))
                    throw new Exception("密码不能为空！");
                if(tbxPassword.Text != tbxConfirm.Text)
                    throw new Exception("密码与确认密码不同！");
                if(tbxCompanyName.EditValue ==null)
                    throw new Exception("单位名必须填写！");
                if(tbxContact.EditValue==null)
                    throw new Exception("联系人必须填写！");
                if(tbxMobPhone.EditValue==null)
                    throw new Exception("手机号必须填写！");
                if(tbxTaxNumber.EditValue==null)
                    throw new Exception("税号必须填写！");
                if(tbxAddress.EditValue==null)
                    throw new Exception("联系地址必须填写！");
                if(tbxBankName.EditValue==null)
                    throw new Exception("开户行必须填写！");
                if(tbxBankNumber.EditValue==null)
                    throw new Exception("银行账号必须填写！");
                if(tbxTel.EditValue==null)
                    throw new Exception("座机电话必须填写！");
                if(cbxProvince.SelectedValue ==null)
                    throw new Exception("所在省必须填写！");
                if(cbxCity.SelectedValue==null)
                    throw new Exception("所在市必须填写！");
                if(cbxDistrict.SelectedValue==null)
                    throw new Exception("所在县区必须填写！");
                if(cbxSBProvince.SelectedValue ==null)
                    throw new Exception("所属省级税务机关必须选择！");
                if(cbxSBCity.SelectedValue==null)
                    throw new Exception("所属市级税务机关必须选择！");
                if(cbxSBDistrict.SelectedValue==null)
                    throw new Exception("所属县区局税务机关必须选择！");
                UserInfoModel um = new UserInfoModel();
                um.Id = 0;
                um.LoginName = tbxUserName.Text.Trim();
                um.Password = tbxPassword.Text.Trim();
                um.CompanyName = tbxCompanyName.Text.Trim();
                um.ContactName = tbxContact.Text.Trim();
                um.MobPhone = tbxMobPhone.Text.Trim();
                um.TaxNumber = tbxTaxNumber.Text.Trim();
                um.Address = tbxAddress.Text.Trim();
                um.BankName = tbxBankName.Text.Trim();
                um.BankNumber = tbxBankNumber.Text.Trim();
                um.TelNumber = tbxTel.Text.Trim();
                um.ProvinceId = Convert.ToInt32(cbxProvince.SelectedValue);
                um.ProvinceName = cbxProvince.Text;
                um.CityId = Convert.ToInt32(cbxCity.SelectedValue);
                um.CityName = cbxCity.Text;
                um.DistrictId = Convert.ToInt32(cbxDistrict.SelectedValue);
                um.DistrictName = cbxDistrict.Text;
                um.SBProvinceId = Convert.ToInt32(cbxSBProvince.SelectedValue);
                um.SBProvinceName = cbxSBProvince.Text;
                um.SBCityId = Convert.ToInt32(cbxSBCity.SelectedValue);
                um.SBCityName = cbxSBCity.Text;
                um.SBDistrictId = Convert.ToInt32(cbxSBDistrict.SelectedValue);
                um.SBDistrictName = cbxSBDistrict.Text;
                RetModel rm = Share.PostData("KPAPI/User/Register", null, um);
                if(!rm.Success)
                {
                    MessageBox.Show("注册失败！\r\n错误代码:" + rm.ErrCode +"\r\n错误信息："+rm.ErrInfo);
                    return;
                }
                MessageBox.Show("注册成功！");
                Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FrmRegister_Shown(object sender, EventArgs e)
        {
            try
            {
                isInit = true;
                //读取地区列表
                RetModel rm = Share.PostData("KPAPI/Base/GetProvince", null, null);
                if(!rm.Success)
                {
                    MessageBox.Show("读取省份列表失败！\r\n错误代码:"+rm.ErrCode+"\r\n错误信息:"+rm.ErrInfo);
                }
                else
                {
                    AreaProvinceModel[] pr = JsonConvert.DeserializeObject<AreaProvinceModel[]>(rm.Data.ToString());
                    prList = pr.ToList();
                }
                //读取税务机关列表
                rm = Share.PostData("KPAPI/Base/GetSBProvince", null, null);
                if(!rm.Success)
                {
                    MessageBox.Show("读取税务机关列表失败！\r\n错误代码:"+rm.ErrCode+"\r\n错误信息:"+rm.ErrInfo);
                }
                else
                {
                    SBProvinceModel[] sbpr = JsonConvert.DeserializeObject<SBProvinceModel[]>(rm.Data.ToString());
                    sbprList = sbpr.ToList();
                }
                //绑定数据源
                cbxProvince.DisplayMember = "Name";
                cbxProvince.ValueMember = "Id,CityList";
                cbxProvince.DataSource = prList;
                cbxProvince.SelectedIndex = -1;

                cbxSBProvince.DisplayMember = "Name";
                cbxSBProvince.ValueMember = "id,CityList";
                cbxSBProvince.DataSource = sbprList;
                cbxSBProvince.SelectedIndex = -1;
                isInit = false;
            }
            catch (Exception ex)
            {
                isInit = false;
                MessageBox.Show(ex.Message);
                Dispose();
            }
        }

        private void cbxProvince_SelectedValueChanged(object sender, EventArgs e)
        {
            if (isInit)
                return;
            try
            {
                isInit = true;
                var obj = cbxProvince.SelectedValue;
                if(null==obj)
                {
                    cbxCity.DataSource = null;
                    isInit = false;
                    return;
                }
                cbxCity.DataSource = null;
                var cl = (AreaProvinceModel)cbxProvince.SelectedValue;
                ctList = cl.CityList.ToList();                
                cbxCity.DisplayMember = "Name";
                cbxCity.ValueMember = "id,DistrictList";
                cbxCity.DataSource = ctList;
                cbxCity.SelectedIndex = -1;
                isInit = false;
            }
            catch (Exception)
            {
                isInit = false;
                throw;
            }
        }

        private void cbxCity_SelectedValueChanged(object sender, EventArgs e)
        {
            if (isInit)
                return;
            try
            {
                isInit = true;
                var obj = cbxCity.SelectedValue;
                if (null==obj)
                {
                    cbxDistrict.DataSource = null;
                    isInit = false;
                    return;
                }
                var ct = (AreaCityModel)cbxCity.SelectedValue;
                dtList = ct.DistrictList.ToList();
                cbxDistrict.DisplayMember = "Name";
                cbxDistrict.ValueMember = "id";
                cbxDistrict.DataSource = dtList;
            }
            catch (Exception)
            {
                isInit = false;
                throw;
            }
        }

        private void cbxSBProvince_SelectedValueChanged(object sender, EventArgs e)
        {
            if (isSBInit)
                return;
            try
            {
                var obj = cbxSBProvince.SelectedValue;
                if(null==obj)
                {
                    cbxSBCity.DataSource = null;
                    isSBInit = false;
                    return;
                }
                sbctList = ((SBProvinceModel)obj).CityList.ToList();
                cbxSBCity.DisplayMember = "Name";
                cbxSBCity.ValueMember = "id,DistrictList";
                cbxSBCity.DataSource = sbctList;
                cbxSBCity.SelectedIndex = -1;
                cbxSBDistrict.DataSource = null;
                isSBInit = false;
            }
            catch (Exception)
            {
                isSBInit = false;
                throw;
            }
        }

        private void cbxSBCity_SelectedValueChanged(object sender, EventArgs e)
        {
            if (isSBInit)
                return;
            try
            {
                isSBInit = true;
                var obj = cbxSBCity.SelectedValue;
                if(null==obj)
                {
                    cbxSBDistrict.DataSource = null;
                    isSBInit = false;
                    return;
                }
                sbdtList = ((SBCityModel)obj).DistrictList.ToList();
                cbxSBDistrict.DisplayMember = "Name";
                cbxSBDistrict.ValueMember = "id";
                cbxSBDistrict.DataSource = sbdtList;
                cbxSBDistrict.SelectedIndex = -1;
                isSBInit = false;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
